<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column
                label="序号"
                width="120"
                type="index"
              />
              <el-table-column
                label="角色名称"
                width="240"
                prop="name"
              />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerms((row.id))">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <!-- @size-change="handleSizeChange" -->
              <el-pagination
                :current-page="q.page"
                :page-sizes="[3, 5, 8,10]"
                :page-size="q.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="hSizeChange"
                @current-change="hCurrentChange"
              />
            </el-row>
            <!-- 新增角色和编辑角色dialog -->
            <el-dialog
              :title="isEdit?'编辑':'添加'"
              :close-on-click-modal="false"
              :close-on-press-escape="false"
              :visible.sync="showDialog"
              @closed="hClose"
            >
              <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="roleForm.name" />
                </el-form-item>
                <el-form-item label="角色描述">
                  <el-input v-model="roleForm.description" />
                </el-form-item>
              </el-form>
              <!-- 底部 -->
              <el-row slot="footer" type="flex" justify="center">
                <el-col :span="6">
                  <el-button size="small" @click="hCancel">取消</el-button>
                  <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
                </el-col>
              </el-row>
            </el-dialog>
            <!-- 分配角色的dialog  -->
            <el-dialog
              title="分配权限弹层"
              :close-on-click-modal="false"
              :close-on-press-escape="false"
              :visible.sync="showDialogAssign"
            >
              <assign-perms v-if="showDialogAssign" :current-id="currentId" @cancel="showDialogAssign=false" /></el-dialog>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { addRole, delRoleById, editRole, getRoleList } from '@/api/role'
import assignPerms from './assignPerms.vue'
export default {
  name: 'Role',
  components: { assignPerms },
  data() {
    return {
      q: {
        page: 1,
        pagesize: 3
      },
      isEdit: false,
      total: 0,
      roleList: [],
      showDialog: false,
      currentId: '',
      showDialogAssign: false,
      roleForm: {
        name: '',
        description: '',
        state: 0
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loadGetROleList()
  },
  methods: {
    // 获取所有的角色
    async loadGetROleList() {
      const { data } = await getRoleList(this.q)
      this.roleList = data.rows
      this.total = data.total
    },
    // 点击删除
    async  hDel(id) {
      // 提示用户是否要删除
      const re = await this.$confirm('您确认要删除吗?', '提示', {
        type: 'warning'
      }).catch(e => e)
      // console.log(re)
      if (re === 'cancel') return
      const res = await delRoleById(id)
      // console.log(res)
      // 提示用户
      this.$message.success(res.message)
      // 要先判断是不是当前的最后一条
      if (this.roleList.length === 1 && this.q.page > 1) {
        this.q.page--
      }
      // 从新渲染页面
      this.loadGetROleList()
    },
    // 点击新增角色
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 点击编辑角色
    hEdit(row) {
      this.showDialog = true
      this.isEdit = true
      // 数据回填
      this.roleForm = row
      // console.log(row)
    },
    // 点击取消dialog弹框
    hCancel() {
      this.showDialog = false
    },
    // 关闭dialog
    hClose() {
      // 重置表单
      // 扩展知识: 该方法只会重置有 prop 的表单项 (字段)
      this.$refs.roleForm.clearValidate()
      this.roleForm = {
        name: '',
        description: '',
        state: 0
      }
      // 关闭弹层
      this.showDialog = false
    },
    // 点击确认
    async   hSubmit() {
      // 兜底校验
      await this.$refs.roleForm.validate()
      // 如果是编辑则发编辑的请求，否则发添加的请求
      let res = null
      // 发请求前先判断是编辑还是添加
      if (this.isEdit) {
      // 编辑的请求
        res = await editRole(this.roleForm).catch(e => this.$message.error(e.message))
      } else {
        //  添加的请求
        res = await addRole(this.roleForm).catch(e => this.$message.error(e.message))
        // 新增的数据要在最后一页显示
        // 如果刚好满一页，total要加1
        if (this.total % this.q.pagesize === 0) {
          this.total++
        }
        this.q.page = Math.ceil(this.total / this.q.pagesize)
      }
      if (!res.code) return
      // 提醒用户
      this.$message.success(res.message)
      // 从新渲染页面
      this.loadGetROleList()
      // 关闭dialog
      this.showDialog = false
    },
    // 处理页码大小
    hSizeChange(val) {
      this.q.pagesize = val
      this.loadGetROleList()
    },
    // 当前页变动时候触发的事件
    hCurrentChange(val) {
      this.q.page = val
      this.loadGetROleList()
    },
    // 点击分配角色
    assignPerms(id) {
      this.currentId = id
      this.showDialogAssign = true
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
